using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.Data.Entity.Infrastructure;
using ShopPowerTemplate.Services.Properties;
using ShopPowerTemplate.Entities;

namespace ShopPowerTemplate.Services
{
    public class SubcategoryServiceBase : Service
    {
		public SubcategoryServiceBase(IEntitiesDB entitiesDB) : base(entitiesDB) { }
		
        public virtual Int32 Add(Subcategory subcategory)
        {
            subcategory.Category = EntitiesDB.EntryWithState(subcategory.Category, EntityState.Unchanged);
            EntitiesDB.SubcategorySet.Add(subcategory);
            EntitiesDB.SaveChanges();
            return subcategory.Id;
        }

        public virtual void Change(Subcategory subcategory)
        {
            subcategory.Category = EntitiesDB.EntryWithState(subcategory.Category, EntityState.Unchanged);
		    EntitiesDB.Entry(subcategory).State = EntityState.Modified;

            EntitiesDB.SaveChanges();
        }
		
		public virtual void Remove(int id)
        {
			Subcategory subcategory = Find(id);
            try
            {
                EntitiesDB.SubcategorySet.Remove(subcategory);
                EntitiesDB.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw new RemoveException(string.Format(Resources.RemoveException, subcategory.ToString()));
            }
        }

        public virtual void Remove(Subcategory subcategory)
        {
			Remove(subcategory.Id);
        }        

        public virtual Subcategory Find(int id)
        {
			Subcategory subcategory = 
				EntitiesDB.SubcategorySet
					.Include("Category")
					.Include("Products")
					.FirstOrDefault(x => x.Id == id);
			
			if(subcategory!=null)
			{
				var categoryAux = subcategory.Category; //EF4.1 bug hack
			}
			return subcategory;
        }

        public virtual List<Subcategory> FindAll()
        {
            return EntitiesDB.SubcategorySet.OrderBy(x => x.Description).ToList();
        }
		
		public virtual List<Subcategory> Search(string searchValue)
        {
            return Search(searchValue, string.Empty, true, 0, 0);
        }

        public virtual List<Subcategory> Search(string searchValue, int pageIndex, int pageSize)
        {
            return Search(searchValue, string.Empty, true, pageIndex, pageSize);
        }

        public virtual List<Subcategory> Search(string searchValue, string orderBy)
        {
            return Search(searchValue, orderBy, true, 0, 0);
        }

        public virtual List<Subcategory> Search(string searchValue, string orderBy, int pageIndex, int pageSize)
        {
            return Search(searchValue, orderBy, true, pageIndex, pageSize);
        }

        public virtual List<Subcategory> Search(string searchValue, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize = 10)
        {
            IQueryable<Subcategory> query = EntitiesDB.SubcategorySet.Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
				|| x.Description.Contains(searchValue));
			
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }
		
		public virtual List<Subcategory> Search(Expression<Func<Subcategory, bool>> searchCriteria, string orderBy = "", bool ascending = true, int pageIndex = 0, int pageSize=10)
        {
            IQueryable<Subcategory> query = EntitiesDB.SubcategorySet.Where(searchCriteria);
            
			if(orderBy == string.Empty)
				orderBy = "Id";			
			
			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int SearchCount(string searchValue)
        {
            return EntitiesDB.SubcategorySet.Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
				|| x.Description.Contains(searchValue)).Count();
        }
		
		public virtual List<Subcategory> FindAvailableSubcategoryList<TKey>(List<Subcategory> notAvailableList, string searchValue, System.Linq.Expressions.Expression<Func<Subcategory, TKey>> orderBy, bool ascending, int pageIndex, int pageSize)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            IQueryable<Subcategory> query = EntitiesDB.SubcategorySet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
				|| x.Description.Contains(searchValue));

			query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }

        public virtual int FindAvailableSubcategoryListCount(List<Subcategory> notAvailableList, string searchValue)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            return EntitiesDB.SubcategorySet.Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
				|| x.Description.Contains(searchValue)).Count();
        }
	
		public virtual void AddProducts(Subcategory subcategory, Product products)
        {
			subcategory = Find(subcategory.Id);
            products.Subcategory = subcategory;
            subcategory.Products.Add(products);

            Change(subcategory);
        }
		
		public virtual void RemoveProducts(Subcategory subcategory, Product products)
        {
            subcategory.Products.Remove(products);
			EntitiesDB.ProductSet.Remove(products);			
            Change(subcategory);
        }

    }
}
